/*
 * File:    WebplasmRenderHook.java
 * Created: 05-Feb-2009
 * Version: $Id$
 *
 * COPYRIGHT (C) 2007, Bitgate Software, LLC.  All Rights Reserved.
 *
 * software@bitgatesoftware.com
 */

package com.bitgate.util.service.protocol.http.render.hook;

import static com.bitgate.util.debug.Debug.debug;
import static com.bitgate.util.debug.Debug.isDebugEnabled;

import com.bitgate.util.service.protocol.http.HttpClientContext;
import com.bitgate.util.service.protocol.http.render.RenderHookException;
import com.bitgate.util.service.protocol.http.render.RenderHookInterface;
import com.bitgate.util.service.protocol.http.render.RenderHookResult;
import com.webplasm.context.RenderContext;
import com.webplasm.render.convert.Convert;
import com.webplasm.render.engine.RenderEngine;
import com.webplasm.render.engine.RenderException;

/**
 * This class implements a rendering agent for Webplasm template pages.
 * 
 * @author kenji
 * @since Webplasm 2.0
 */
public class WebplasmRenderHook implements RenderHookInterface
{
	/**
	 * No argument constructor.
	 */
	public WebplasmRenderHook()
	{
		if (isDebugEnabled()) {
			debug("Webplasm Render Hook constructed.");
		}
	}
	
	/**
	 * Renders the specified filename with the given {@link HttpClientContext} object.
	 * 
	 * @param file The <code>String</code> containing the filename to render.
	 * @param hcContext The currently active <code>HttpClientContext</code> object.
	 * @return <code>RenderHookResult</code> containing the result.
	 * @throws RenderHookException on any errors.
	 */
	public RenderHookResult renderPage(String file, final HttpClientContext hcContext) throws RenderHookException
	{
		String convertedPage = Convert.convertOutput(file);
		
		if (convertedPage == null) {
			throw new RenderHookException("Unable to render page, Webplasm could not convert the output.");
		}
		
		RenderEngine renderEngine = new RenderEngine();
		RenderContext renderContext = new RenderContext(hcContext);
		RenderHookResult rhResult = new RenderHookResult();
		
		// TODO: Better exception handling here, show the actual exception instead of a pre-formatted text message.
		
		try {
			StringBuffer buf = renderEngine.render(convertedPage.getBytes(), renderContext);
		
			if (buf != null) {
				rhResult.setRenderedData(buf.toString().getBytes());
				rhResult.setContentType(renderContext.getContentType());
				
				return rhResult;
			}
		} catch(RenderException ex) {
			StringBuffer buf = new StringBuffer();
			
			buf.append("<html><head><title>500</title></head><body>")
			   .append("Your document failed to render due to an exception that occurred while processing the request:<p/><pre>")
			   .append(ex.getMessage())
			   .append("</pre></body></html>");
			
			rhResult.setRenderedData(buf.toString().getBytes());
			rhResult.setContentType("text/html");
			
			return rhResult;
		}
		
		rhResult.setRenderedData("Your document failed to render.".getBytes());
		rhResult.setContentType("text/html");
		
		return rhResult;
	}
}